﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;
using CI.Interfaces;
using CI;
using CI.Organism;

namespace CI.PW
{
    /// <summary>
    /// Interaction logic for Genes2D.xaml
    /// </summary>
    public partial class Genes2D : UserControl
    {
        private Dispatcher _uiDispatcher;

        /// <summary>
        /// Genes in a 2D display
        /// </summary>
        public Genes2D()
        {
            InitializeComponent();
            _uiDispatcher = Dispatcher.CurrentDispatcher;
            SimEnv.Instance.OnSetSelectedOrganismEvent += DisplaySelectedItem;
        }

        /// <summary>
        /// Set the item to display
        /// </summary>
        /// <param name="organism"></param>
        public void DisplaySelectedItem(IOrganism organism)
        {
            #region Argument Checker
            if (!(organism is IOrganism))
                return;
            #endregion

            _uiDispatcher.BeginInvoke(new Action(delegate()
            {
                // clear the list
                treeView.Items.Clear();

                // clear the text list
                label1.Content = null;

                // add the chromosomes
                foreach (IChromosome chromosome in organism.DNA.Chromosomes)
                {
                    TreeViewItem _tvChromosome = new TreeViewItem();
                    _tvChromosome.Header = chromosome.GetType();
                    _tvChromosome.Selected += new RoutedEventHandler(onSelectedChromsomse);
                    _tvChromosome.Tag = chromosome;
                    treeView.Items.Add(_tvChromosome);

                    // add the genes
                    foreach (IGene gene in chromosome.Genes)
                    {
                        TreeViewItem _tvGene = new TreeViewItem();
                        _tvGene.Header = gene.GetType();
                        _tvGene.Selected += new RoutedEventHandler(onSelectedGene);
                        _tvGene.Tag = gene;
                        _tvChromosome.Items.Add(_tvGene);


                        // add the requirements
                        foreach (IRequirement requirement in gene.Requirements)
                        {
                            TreeViewItem _tvRequirement = new TreeViewItem();
                            _tvRequirement.Header = requirement.GetType();
                            _tvRequirement.Selected += new RoutedEventHandler(onSelectedRequirement);
                            _tvRequirement.Tag = requirement;
                            _tvGene.Items.Add(_tvRequirement);
                        }
                    }

                }
            }));
        }

        /// <summary>
        /// On selected chromsome
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void onSelectedChromsomse(Object sender, RoutedEventArgs e)
        {
            #region argument checks
            if (sender == null)
                return;

            if (!(sender is TreeViewItem))
                return;

            if (!((TreeViewItem)sender).IsSelected)
                return;

            if (!((((TreeViewItem)sender).Tag) is IChromosome))
                return;
            #endregion

            // cast it
            IChromosome chromsome = ((IChromosome)((TreeViewItem)sender).Tag);

            // set values
            label1.Content = chromsome.ToString() + System.Environment.NewLine;
        }


        /// <summary>
        /// On selected gene
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void onSelectedGene(Object sender, RoutedEventArgs e)
        {
            #region argument checks
            if (sender == null)
                return;

            if (!(sender is TreeViewItem))
                return;

            if (!((TreeViewItem)sender).IsSelected)
                return;

            if (!((((TreeViewItem)sender).Tag) is IGene))
                return;
            #endregion

            // cast it
            IGene gene = ((IGene)((TreeViewItem)sender).Tag);

            // set values
            label1.Content = gene.ToString() + System.Environment.NewLine +
                            string.Format("Action Type : {0}", gene.ActionType) + System.Environment.NewLine +
                            string.Format("Actioned : {0}", gene.Actioned) + System.Environment.NewLine +
                            string.Format("Requirements : {0}", gene.Requirements.Count);
        }


        /// <summary>
        /// On selected requirements
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void onSelectedRequirement(Object sender, RoutedEventArgs e)
        {
            #region argument checks
            if (sender == null)
                return;

            if (!(sender is TreeViewItem))
                return;

            if (!((TreeViewItem)sender).IsSelected)
                return;

            if (!((((TreeViewItem)sender).Tag) is IRequirement))
                return;
            #endregion

            // cast it
            IRequirement requirement = ((IRequirement)((TreeViewItem)sender).Tag);

            // set values
            label1.Content = requirement.ToString();
        }
    }
}
